package com.example.coffeejava.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.coffeejava.Dto.ExamineDto;
import com.example.coffeejava.Dto.Result;
import com.example.coffeejava.Entity.Examine;
import com.example.coffeejava.Entity.Shop;
import com.example.coffeejava.Entity.UserHolder;
import com.example.coffeejava.Service.ExamineService;
import com.example.coffeejava.Service.ShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/examine")
public class ExamineController {
    @Resource
    private ExamineService examineService;
    @Resource
    private ShopService shopService;

    @GetMapping("/list")
    public Result list(Integer pageNum,Integer pageSize,String keyword){
        return examineService.getlist(pageNum,pageSize,keyword);

    }
    @GetMapping("/certify")
    public Result certify(Long id,String status,String remark,Long shopId){
        Shop shop=shopService.getById(shopId);
        if(shop == null){
            return Result.error("找不到店铺");
        }
        if(status.equals("通过")){
            shop.setCertified("已认证");
            shopService.updateById(shop);
        }

        Examine examine=examineService.getById(id);
        examine.setStatus(status);
        examine.setRemark(remark);
        examineService.updateById(examine);
        return Result.ok("成功");


    }
    @PostMapping("/add")
    public Result add(@RequestBody ExamineDto examineDto) {
        Shop shop = shopService.getById(UserHolder.getUser().getId());
        LambdaQueryWrapper<Examine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Examine::getStatus, "待审核");
        lambdaQueryWrapper.eq(Examine::getShopId, UserHolder.getUser().getId());
        Examine examine1 = examineService.getOne(lambdaQueryWrapper);

        if (shop.getCertified().equals("已认证")) {
            return Result.error("认证已通过，请勿重复认证");
        } else if (examine1 !=null && examine1.getStatus().equals("待审核")) {
//            if(examine1.getStatus().equals("待审核")){
   return Result.error("请勿重复提交");
//
//            }else{
//                return Result.ok(0);
//            }
        } else {
            Examine examine = new Examine();
            examine.setIdentityFile(examineDto.getIdentityFile());
            examine.setIdentityNum(examineDto.getIdentityNum());
            examine.setShopId(UserHolder.getUser().getId());
            examine.setShopName(UserHolder.getUser().getNickName());
            examine.setStatus("待审核");
            examine.setQualificationFile(examineDto.getQualificationFile());
            examineService.save(examine);
            return Result.ok("成功");
        }

    }
}
